import handlebars from 'handlebars'
import lodash from 'lodash'
const { camelCase, upperFirst } = lodash
// plopfile.mjs
export default function (plop) {
  // plop generator code
  // 注册小驼峰（camelCase）帮助器
  handlebars.registerHelper('camelCase', function (text) {
    return camelCase(text)
  })
  // 注册大驼峰（PascalCase）帮助器
  handlebars.registerHelper('pascalCase', function (text) {
    return upperFirst(camelCase(text))
  })
  plop.setGenerator('store', {
    // 描述
    description: 'generate store',
    prompts: [
      {
        type: 'input', // 用户输入
        name: 'name', // 接收的变量名
        message: 'store name please' // 提示用户输入信息
      }
    ],
    actions: [
      {
        type: 'add', // 新建文件
        path: 'src/stores/modules/{{ name }}.ts', // 新建文件的路径
        templateFile: 'plop-templates/store/module.ts.hbs' // 模板路径
      },
      {
        type: 'modify', // 修改文件
        path: 'src/stores/index.ts', // 修改文件路径
        pattern: /(\/\/ -- append store here --)/gi, // 正则找到标识位置(在文件哪里修改)
        templateFile: 'plop-templates/store/index.ts.hbs' // 模板路径
      }
    ]
  })

  plop.setGenerator('view', {
    description: 'generate view',
    prompts: [
      {
        type: 'input',
        name: 'name',
        message: 'view name please'
      }
    ],
    actions: [
      {
        type: 'add',
        path: 'src/views/{{ camelCase name }}/index.vue', // properCase大驼峰，详细请看文档
        templateFile: 'plop-templates/view/index.vue.hbs'
      },
      {
        type: 'add',
        path: 'src/router/modules/{{ camelCase name }}.ts',
        templateFile: 'plop-templates/view/route.ts.hbs'
      },
      {
        type: 'modify',
        path: 'src/router/index.ts',
        pattern: /(\/\/ -- append import here --)/gi,
        templateFile: 'plop-templates/view/import.ts.hbs'
      },
      {
        type: 'modify',
        path: 'src/router/index.ts',
        pattern: /(\/\/ -- append router here --)/gi,
        templateFile: 'plop-templates/view/router.ts.hbs'
      }
    ]
  })
  plop.setGenerator('component', {
    description: 'generate component',
    prompts: [
      {
        type: 'input',
        name: 'name',
        message: 'component name please'
      }
    ],
    actions: [
      {
        type: 'add',
        path: 'src/components/Cp{{ name }}.vue',
        templateFile: 'plop-templates/component/index.vue.hbs'
      },
      {
        type: 'modify',
        path: 'src/types/components.d.ts',
        pattern: /(\/\/ -- append import here --)/gi,
        templateFile: 'plop-templates/component/import.ts.hbs'
      },
      {
        type: 'modify',
        path: 'src/types/components.d.ts',
        pattern: /(\/\/ -- append type here --)/gi,
        templateFile: 'plop-templates/component/type.ts.hbs'
      }
    ]
  })
}
